package com.test.imu;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class RotatableView extends View
{
	public static final String TAG = "TEST";
	private boolean mInitialized = false;
	private float startX, startY, startZ, x,y,z;
	private Paint mPaint;
	private Rect mRect;
	private Path mPath;
	
	private Activity mContext;
	private float horizontalMargin;
	private float verticalMargin;
	
	public RotatableView(Context context) {
		super(context);

		init(context);
	}

	public RotatableView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		
		init(context);
	}

	public RotatableView(Context context, AttributeSet attrs) {
		super(context, attrs);

		init(context);
	}
	
	private void init(Context context)
	{
		mContext = (Activity) context;
		mPaint = new Paint();
		mPaint.setColor(Color.BLUE);
		mPaint.setStrokeWidth(20);
		mPaint.setStyle(Paint.Style.FILL);
		
		mRect = new Rect(250,200,500,300);
		
		setWillNotDraw(false);
		
		setLayoutParams(new LayoutParams(0,0));
		
		setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//		setBackgroundColor(Color.RED);
//		setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);		
		
		mPath = new Path();
		
		setWillNotCacheDrawing(true);
		setWillNotDraw(false);
		invalidate();
	}
	
	public void setRotation(float newX, float newY, float newZ)
	{
//		Log.d(TAG, String.format("%f - %f - %f", x,y,z));
		if(!mInitialized)
		{
			startX = newX;
			startY = newY;
			startZ = newZ;
			mInitialized = true;
		}
		this.x = startX - newX;
		this.y = startY - newY;
		this.z = startZ + newZ;
		
//		horizontalMargin = mRect.width() * (y/360f);
		horizontalMargin = Math.abs(mRect.width() * (x/360));
//		verticalMargin = mRect.height() * (x/360f);
		verticalMargin = Math.abs(mRect.height() * (y/360f));
		
		TextView tvX= (TextView) mContext.findViewById(R.id.rel_x_axis);
		TextView tvY= (TextView) mContext.findViewById(R.id.rel_y_axis);
		TextView tvZ= (TextView) mContext.findViewById(R.id.rel_z_axis);
		tvX.setText(String.format("%d", (int) x));
		tvY.setText(String.format("%d", (int) y));
		tvZ.setText(String.format("%d", (int) z));
		
		setWillNotCacheDrawing(true);
		setWillNotDraw(false);
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas) 
	{
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		canvas.save();
		
//		canvas.rotate(z, mRect.exactCenterX(), mRect.exactCenterY());
		
//		Rect refRect = new Rect(mRect);
//		refRect.offsetTo(250, 500);
//		
//		canvas.drawRect(refRect, mPaint);
//		drawRectByPath();
//		canvas.drawPath(mPath,mPaint);
		canvas.restore();
//		
	}

	private void drawRectByPath() 
	{
		mPath.reset();
		
		
		//250,200,500,300
//		mPath.moveTo(250, 200);
//		mPath.lineTo(500, 200);
//		mPath.lineTo(500, 300);
//		mPath.lineTo(250, 300);
//		mPath.lineTo(250,200);
		
//		float tlx = y < 0 ? mRect.left - horizontalMargin : 0;
//		float tly = x < 0 ? mRect.top - verticalMargin : 0;
		
		mPath.moveTo(mRect.left - horizontalMargin, mRect.top - verticalMargin);
		mPath.lineTo(mRect.right + horizontalMargin, mRect.top - verticalMargin);
		mPath.lineTo(mRect.right + horizontalMargin, mRect.bottom + verticalMargin);
		mPath.lineTo(mRect.left - horizontalMargin, mRect.bottom + verticalMargin);
		
//		mPath.moveTo(mRect.left - horizontalMargin, mRect.top + verticalMargin);
//		mPath.lineTo(mRect.right + horizontalMargin, mRect.top - verticalMargin);
//		mPath.lineTo(mRect.right - horizontalMargin, mRect.bottom + verticalMargin);
//		mPath.lineTo(mRect.left + horizontalMargin, mRect.bottom - verticalMargin);
		
//		mPath.moveTo(mRect.left + Math.abs((mRect.width() * (x/90))), mRect.top + Math.abs((mRect.height() * (y/90))));
//		mPath.lineTo(mRect.right -  Math.abs((mRect.width() * (x/90))), mRect.top + Math.abs((mRect.height() * (y/90))));
//		mPath.lineTo(mRect.right, mRect.bottom);
//		mPath.lineTo(mRect.left, mRect.bottom);
//		mPath.lineTo(mRect.left, mRect.top);
		
	}
}
